package org.blogwu.learn.threadLearn.自己实现一个重入锁.使用AQS;

public class MyLockTest {
    static MyLock reentrantLock = new MyLock();

    // 必须加volatile，否则有可见性问题
    static volatile int i = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 0; j < 2000; j++) {
                    reentrantLock.lock();
                    i++;
                    reentrantLock.unlock();
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 0; j < 2000; j++) {
                    reentrantLock.lock();
                    i++;
                    reentrantLock.unlock();
                }
            }
        });
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("i= " + i);
    }
}
